﻿using Auto.Tools.Standards.Extends;

using System;
using System.Collections.Generic;

using static Auto.Tools.Standards.EnumCollection;

namespace Auto.Tools.Standards.ServiceResult
{
    /// <summary>
    /// 服务层响应返回数据
    /// </summary>
    public class ServiceResultBase : IPageDataBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="code">默认：Exception（异常）</param>
        /// <param name="message">默认：string.Empty</param>
        public ServiceResultBase(ResultCode? code, string message, string prevURI, int total = 0, int pageSize = 10, int pageIndex = 1)
        {
            Code = code ?? ResultCode.Exception;
            Message = message ?? string.Empty;
            PrevURI = prevURI ?? "/Home/Index";
            Total = total;
            PageSize = pageSize;
            PageIndex = pageIndex;

        }

        /// <summary>
        /// 默认：code=Unknown（未知错误），Message=string.Empty
        /// </summary>
        public ServiceResultBase()
        {
            Code = ResultCode.Unknown;
            Message = string.Empty;
            PrevURI = "/Home/Index";
            Total = 0;
            PageSize = 10;
            PageIndex = 1;
        }

        /// <summary>
        /// 响应状态code
        /// </summary>
        public ResultCode Code { get; private set; }
        /// <summary>
        /// 响应消息
        /// </summary>
        public string Message { get; private set; }
        /// <summary>
        /// 其他返回信息
        /// </summary>
        public object JsonData { get; set; }

        /// <summary>
        /// 前一个请求地址
        /// </summary>
        public string PrevURI { get; set; }
        /// <summary>
        /// 成功
        /// </summary>
        public bool Successful => Code == ResultCode.Successful;

        /// <summary>
        /// 时间戳(毫秒)
        /// </summary>
        public long Timestamp { get; } = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;

        /// <summary>
        /// 查询条件
        /// </summary>
        public Dictionary<string, object> SearchData { get; set; }
        /// <summary>
        /// 数据总条数
        /// </summary>
        public int Total { get; set; }
        /// <summary>
        /// 页码大小
        /// </summary>
        public int PageSize { get; set; }
        /// <summary>
        /// 当前页索引
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 响应成功
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public void IsSuccesful(string message = "")
        {
            Message = message.IsNotNull() ? message : Constants.ResultMessage.SuccessfulMessage;
            Code = ResultCode.Successful;
        }

        /// <summary>
        /// 响应失败
        /// </summary>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public void IsFailedOrException(string message = "", string prevURI = "/Home/Index", Exception exception = null, ResultCode resultCode = ResultCode.Failed)
        {
            if (exception is null)
            {
                Message = message.IsNotNull() ? message : Constants.ResultMessage.OtherMessage;
                Code = resultCode;
                PrevURI = prevURI;
            }
            else
            {
                Message = exception.InnerException?.StackTrace;
                Code = ResultCode.Exception;
                PrevURI = prevURI;
            }

        }
    }
}
